<template>
    <div class="myMoney-main">
        <div class="header-wrapper">
            <div class="com-header">
                <button class="back" @click="$router.go(-1)"><i></i></button>
                <span>我的现金</span>
                <button class="clear-all" @click="ruleTool=true">规则</button>
            </div>
            <p class="title">现金账户（元）</p>
            <div class="content">
                <span class="count">{{balance}}</span>
                <button class="putforward-btn" @click="putForward">提现</button>
            </div>
            <p class="footer">
                累计阅读时长：{{allTime | durationFilter}}分钟
            </p>
        </div>
        
        <!-- 网页内容区域 -->
        <div class="wrapper">
             <ul v-if="isHaveBookTool==1">
                 <li v-for="(obj,key) in moneyRecordArr" :key="key" class="day-item">
                    <div class="header" @click="obj.spread_flag=!obj.spread_flag">
                         <span class="date">{{key}}</span>
                         <i class="more-icon" :class="{'show-more':obj.spread_flag==0}"></i>
                    </div>
                    <div class="content" v-if="obj.spread_flag==0">
                        <ul>
                            <li v-for="(item,index) in obj.detail" :key="index" class="align-center">
                                <span class="left">
                                    <p>阅读时长达到  {{item.duration | durationFilter}} 分钟</p>
                                    <p>{{item.generate_time | timeFilter}}</p>
                                </span>
                                <p class="money"><span class="num">{{item.money | moneyFilter}}</span></p>
                            </li>
                        </ul>
                    </div>
                 </li>
             </ul>
             <div class="no-wrapper" v-if="isHaveBookTool==0">
                 <i></i>
                 <button @click="lookBook"></button>
             </div>
        </div>
        <div class="cover full-screen-window" v-if="ruleTool"></div>
        <div class="rule-wrapper" v-if="ruleTool">
            <div class="header"><em class="cancel" @click="ruleTool=false">X</em></div>
            <div class="content">
                <p v-for="(value,index) in ruleArr" :key="index">{{value}}</p>
            </div>
        </div>
    </div>
</template>

<script>
    import {Toast,Indicator} from 'mint-ui'
    import api from '~/plugins/api.js'
    import axios from 'axios'
    import storage from 'localforage'
    export default {
        head: {
            title: '我的现金  - 快眼看书',
            meta: [{
                name: 'description',
                content: '快眼看书专注于玄幻小说搜索,新版快眼看书,提供最全的小说保持最快的更新,方便大家愉快地阅读玄幻小说'
            },{
                name: 'keyWords',
                content: '我的现金,快眼看书'
            }]
        },
        filters: {
            durationFilter: function (value) {
                let num = parseInt(value)
                num = num/60
                return num;
            },
            moneyFilter: function(value) {
                let money = `+${value}.00`
                return money
            },
            timeFilter: function(value) {
                let timeArr = value.split(' ')
                if(timeArr.length==2){
                    return timeArr[1]
                }else{
                    return value
                }
            }
        },
        data () {
            return {
              dataArr: [],
              userinfo: {},
              ruleTool: false,
              ruleArr: [],
              balance: '0.00',
              moneyRecordArr: {},
              allTime: 0,
              isHaveBookTool: 2
            }
        },
        mounted() {

            this.initData()
        },
        methods: {
            clearHistory () {    
                storage.removeItem('saveReadHistoryKey')
                this.dataArr = []
            },
            async initData() {
                this.getWithdrawRules()
                this.userinfo = await storage.getItem('userinfo')
                if(this.userinfo){
                    this.getMoneyRecord()
                }
            },
            /**
             * 我的现金
             */
            async getMoneyRecord() {
                let paramsData = {
                    uid: this.userinfo.uid,
                    token: this.userinfo.token
                }
                let result = await axios.get(api.moneyRecord,{params:paramsData})
                if(result) {
                    this.moneyRecordArr = result.data.data.log
                    if(Object.keys(this.moneyRecordArr).length){
                        this.isHaveBookTool = 1
                    }else{
                        this.isHaveBookTool = 0
                    }
                    this.allTime = result.data.data.duration_sum
                    this.balance = result.data.data.balance
                }
            },
            /**
             * 提现规则
             */
            async getWithdrawRules() {
                let result = await axios.get(api.withdrawRules)
                if(result) {
                    this.ruleArr = result.data.data.rule
                }
            },
            /**
             * 提现
             */
            async putForward() {
                if(this.userinfo){
                    let paramsData = {
                        uid: this.userinfo.uid,
                        token: this.userinfo.token
                    }
                    Indicator.open({ spinnerType: 'triple-bounce'});
                    let result = await axios.get(api.withdrawAction,{params:paramsData})
                    Indicator.close();
                    if(result) {
                        if(result.data.status==1){
                            let res = result.data.data
                            let token = res.token
                            let uid = res.uid
                            let name = 'ky'+this.userinfo.uid
                            window.location.href = `https://m.uedvv.com/kyks/kyks.html?token=${token}&uid=${uid}&usename=${name}`
                        }else{
                            Toast(result.data.info)
                        }
                    }
                }else{
                    Toast('请先登录')
                }
                
            },
            async lookBook() {
                let isHaveBook = await storage.getItem('bookshelfIdKey') || []
                if(isHaveBook.length){
                    this.$router.push('/bookshelf')
                }else{
                    this.$router.push('/')
                }
                
            }
        }
    }
</script>
